package cn.cvs.web;

import cn.cvs.service.StorageRecordService;
import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.Supplier;
import cn.cvs.service.SysSupplierService;
import cn.cvs.util.Constants;
import cn.cvs.util.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class StorageRecordController {
    @Resource
    StorageRecordService storageRecordService;
    @Resource
    SysSupplierService sysSupplierService;

    @RequestMapping("/sys/storageRecord/list")
    public String list(@RequestParam(value = "queryGoodsName",required = false) String goodsName,
                       @RequestParam(value = "querySupplierId",required = false) Integer supplierId,
                       @RequestParam(value = "queryPayStatus",required = false) Integer payStatus,
                       @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageNo,
                       Model model){
        //出参 queryRealName
        model.addAttribute("queryGoodsName",goodsName);
        //出参 queryRoleId
        model.addAttribute("querySupplierId",supplierId);
        //出参 queryRoleId
        model.addAttribute("queryPayStatus",payStatus);
        // 出参 roleList 角色类表(全部的角色)
        List<Supplier> list = sysSupplierService.queryAllSupplier();
        model.addAttribute("supplierList",list);
        //出参 userList  用户列表
        List<StorageRecord> sysUsers
                = storageRecordService.queryStorage(goodsName,supplierId, payStatus,pageNo, Constants.pageSize);
        model.addAttribute("storageRecordList",sysUsers);
        //totalCount (参与条件分页)总记录数
        int count = storageRecordService.queryCount(goodsName, supplierId,payStatus);
        model.addAttribute("totalCount",count);
        //currentPageNo 当前页码 就是入参 pageIndex
        model.addAttribute("currentPageNo",pageNo);
        //totalPageCount 总页数
        PageSupport pageSupport = new PageSupport();
        //设置pageSize 一页几条记录
        pageSupport.setPageSize(Constants.pageSize);
        //设置参与分页的总记录数
        pageSupport.setTotalCount(count);
        //获得总页数
        int totalPageCount = pageSupport.getTotalPageCount();
        model.addAttribute("totalPageCount",totalPageCount);
        return "/storageRecord/list";
    }
}
